<?php
/**
 * 创建分表
 */
class LogTableHandler extends ScriptBase
{
    private $_mysql;
    private $tables = ['log_login','log_user_paiju'];
    private $liketables = ['log_login','log_user_paiju_toku'];
    public function logic()
    {
        $this->_mysql = $this->mysql('logs');
        $nextTime =  date('Ym', strtotime('+1 month'));
        $table_fix = '_'.$nextTime;
        $this->create_table($table_fix);
        
    }
    
    private function create_table($table_fix){
        foreach ($this->tables as $key=>$val){
            $table = $val.$table_fix;
            $likeTable = $this->liketables[$key];
            $hasTable = $this->_mysql->tableExists($table); // 判断是否有该表
            $createFlag = $hasTable ? true : false;
            
            if (!$createFlag) {
                //创建表
                $createSql = "CREATE TABLE {$table} LIKE {$likeTable}";
                $createFlag = $this->_mysql->query($createSql);
            }
            sleep(5);
        }
    }
}